//
//  HomeMenuItemCell.m
//  ShowYork
//
//  Created by hxh on 2021/4/30.
//  Copyright © 2021 hxh. All rights reserved.
//

#import "HomeMenuItemCell.h"

@interface HomeMenuItemCell ()
@property (nonatomic,strong)UILabel *contentLb;
@property (nonatomic,strong)UILabel *subTitleLable;
@property (nonatomic,strong)UIView *line;
@property (nonatomic,strong)UIButton *coverBtn;
@end

@implementation HomeMenuItemCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUI];
    }
    return self;
}
- (void)setUI{
    self.contentLb = [[UILabel alloc]init];
    self.contentLb.textColor = C51;
    self.contentLb.font = font(18);
    [self.contentView addSubview:self.contentLb];
    
    self.subTitleLable = [[UILabel alloc]init];
    self.subTitleLable.textColor = C153;
    self.subTitleLable.font = font(10);
    [self.contentView addSubview:self.subTitleLable];
    
    self.line = [[UIView alloc]init];
    self.line.backgroundColor = RGBA(230, 230, 230, 1);
    [self.contentView addSubview:self.line];
    
    self.coverBtn = [[UIButton alloc]init];
    [self.coverBtn addTarget:self action:@selector(coverMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.coverBtn];
    
    [self.contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(10);
        make.centerX.equalTo(self.contentView);
    }];
    [self.subTitleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLb.mas_bottom);
        make.centerX.equalTo(self.contentView);
    }];
    
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView);
        make.height.mas_equalTo(0.5);
        make.left.equalTo(self.contentView).offset(16);
        make.right.equalTo(self.contentView).offset(-16);
    }];
    
    [self.coverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.contentView);
    }];
    
    self.line.hidden = YES;
}

+(instancetype)cellWithTableView:(UITableView *)tableView{
    NSString *ID = @"HomeMenuItemCell";
    HomeMenuItemCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[HomeMenuItemCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (void)setItemModel:(HomePageMenuItemModel *)itemModel{
    _itemModel = itemModel;
    
    if (itemModel.subTitle.length == 0) {
        [self.contentLb mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(18);
        }];
        self.subTitleLable.text = @"";
    }else{
        [self.contentLb mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(10);
        }];
        self.subTitleLable.text = itemModel.subTitle;
    }
    self.contentLb.text = itemModel.mainTitle;
    
    if (itemModel.isHide) {
        self.line.hidden = YES;
    }else{
        self.line.hidden = NO;
    }
}

- (void)coverMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(showMenuItem:)]) {
        [self.delegate showMenuItem:self.itemModel];
    }
}

@end
